package com.inossem.warehousestorage_inossem.adapter.inventory;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.inventory.InventoryExtra;
import com.inossem.warehousestorage_inossem.activity.inventory.InventoryItemActivity;
import com.inossem.warehousestorage_inossem.activity.inventory.InventoryItemAddActivity;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryAreaBean;
import com.inossem.warehousestorage_inossem.view.StatusTextView;

/**
 * Created by Administrator on 2017/8/12 0012.
 */

public class InventoryOverviewCellAdapter extends BaseAdapter {

    public static final String TYPE_1 = "1";
    private Context mContext;
    private LayoutInflater inflater;
//    private List<InventoryOverviewBean.DataBean.AreaListBean.Box> boxList;
    private InventoryAreaBean.AreaListBean bean;


    public InventoryOverviewCellAdapter(Context context, InventoryAreaBean.AreaListBean bean) {
        mContext = context;
        inflater = LayoutInflater.from(context);
        this.bean = bean;
    }

    @Override
    public int getCount() {
        return bean.getPosition_list().size()+1;
    }

    @Override
    public Object getItem(int position) {
        return bean.getPosition_list().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_inventory_overview_cell, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, position);

        return convertView;
    }


    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.text =  (StatusTextView) view.findViewById(R.id.text);
        viewHolder.newItem =  (TextView) view.findViewById(R.id.newItem);

    }

    private void initData(Context context, ViewHolder viewHolder, View view, int position) {
        if(position==getCount()-1){
            //最后一个，设置加号
            viewHolder.text.setText("+");

            //事件处理
            view.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Intent intent = new Intent(context, InventoryItemAddActivity.class);
                    intent.putExtra(InventoryExtra.AREA_NUMBER_VALUE,bean.getArea_id());
                    context.startActivity(intent);
                }
            });
        }else{
//            InventoryOverviewBean.DataBean.AreaListBean.Box  itemsBean = ( InventoryOverviewBean.DataBean.AreaListBean.Box)getItem(position);
            InventoryAreaBean.AreaListBean.PositionListBean itemsBean = (InventoryAreaBean.AreaListBean.PositionListBean)getItem(position);
            viewHolder.text.setStatus(Integer.parseInt(itemsBean.getStatus()));
            viewHolder.text.setText(itemsBean.getSuffix());
            if(itemsBean.getType().equals(TYPE_1)){
                //加角标
                viewHolder.newItem.setVisibility(View.VISIBLE);
            }else{
                viewHolder.newItem.setVisibility(View.GONE);
            }


            //事件处理
            view.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    if(itemsBean.getType().equals(TYPE_1)){
//                        新增物料进入的界面不一样
                        Intent intent = new Intent(context, InventoryItemAddActivity.class);
                        intent.putExtra(InventoryExtra.AREA_NUMBER_VALUE,bean.getArea_id());
                        intent.putExtra(InventoryExtra.POSITION_NUMBER_VALUE,itemsBean.getPosition_id());
                        intent.putExtra(InventoryExtra.ID,itemsBean.getItem_id());
                        context.startActivity(intent);
                    }else{
                        Intent intent = new Intent(context, InventoryItemActivity.class);
                        intent.putExtra(InventoryExtra.AREA_NUMBER_VALUE,bean.getArea_id());
                        intent.putExtra(InventoryExtra.POSITION_NUMBER_VALUE,itemsBean.getPosition_id());
                        context.startActivity(intent);
                    }

                }
            });
        }


    }

    private class ViewHolder {
        private StatusTextView text;
        private TextView newItem;
    }


}
